﻿using System;
using System.Text;
using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms.Design;

namespace MyFastDataSourceBind
{
    /// <summary>
    /// 编辑数据源字段的扩展编辑器
    /// </summary>
    /// <remarks>编写 袁永福</remarks>
    public class XDataFieldNameEditor : UITypeEditor
    {
        /// <summary>
        /// 初始化对象
        /// </summary>
        public XDataFieldNameEditor()
        {
        }
        /// <summary>
        /// 返回编辑操作样式为下拉列表样式
        /// </summary>
        /// <param name="context">参数</param>
        /// <returns>下拉列表样式</returns>
        public override UITypeEditorEditStyle GetEditStyle(
            ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }
        /// <summary>
        /// 编辑数据源字段名
        /// </summary>
        /// <param name="context">参数</param>
        /// <param name="provider">参数</param>
        /// <param name="Value">当前数值</param>
        /// <returns>编辑后的数值</returns>
        public override object EditValue(
            ITypeDescriptorContext context,
            IServiceProvider provider,
            object Value)
        {
            if (provider == null)
                return Value;
            // 获得服务对象
            IWindowsFormsEditorService svc =
                (IWindowsFormsEditorService)provider.GetService(
                typeof(IWindowsFormsEditorService));
            if (svc == null)
                return Value;
            // 获得当前编辑的对象实例,一般为用户界面控件对象
            object obj = context.Instance;
            if (!(obj is IXDataSourceable))
            {
                // 若对象实例不能转换为 IXDataSourceable 类型则退出处理
                return Value;
            }
            // 获得数据源名称
            string DataSourceName = ((IXDataSourceable)obj).XDataSourceName;
            // 获得指定名称的全局数据源对象
            XDataSource ds = XDataSource.GetInstance(DataSourceName);
            if (ds == null)
            {
                // 若没有找到全局数据源对象则退出
                return Value;
            }
            // 获得指定数据表的字段信息
            XDesignerData.TableInfo tableInfo = ConnectionHelper.DBInfo.Tables[ds.Name];
            if (tableInfo != null && tableInfo.Fields.Count > 0)
            {
                using (DesignPopupListBox lst = new DesignPopupListBox())
                {
                    // 将字段名称和说明信息填充到列表中
                    foreach (XDesignerData.FieldInfo field in tableInfo.Fields)
                    {
                        lst.AddItem(field.Name, field.Description);
                    }
                    // 弹出字段信息列表
                    string v = lst.Popup(svc, Convert.ToString(Value));
                    if (v != null)
                    {
                        // 返回用户选择的字段名
                        return v;
                    }
                }//using
            }
            return Value;
        }//public override object EditValue()

    }//public class XDataFieldUITypeEditor : UITypeEditor
}